﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.Data;
using System.Text;

namespace HQWeb.Procurement
{
    public partial class Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (Request["id"] != null)
            {
                hidPid.Value = Request["id"].ToString();
                hidType.Value = "edit";
                if (Request["sign"] == "2")
                {
                    allow.Style["display"] = "";

                }
                string id = Request["id"];
                BindDate(id);
                //btnSubmit.Text = "确定";
                //btnCancle.Visible = false;
                //btnReturn.Visible = true;
            }
        }
        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
            DataTable modelList = bll.GetListByPid(id).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralName;
            string strNum;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralName = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();

                if (i != count - 1)
                {
                    txtUserIdHid.Value += strMateral + ",";
                    txtUserNameHid.Value += strMateralName + ",";
                }
                else
                {
                    txtUserIdHid.Value += strMateral;
                    txtUserNameHid.Value += strMateralName;
                }

                strHtml.Append("  <table width=\"58%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:10%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:10%\">");
                strHtml.Append(" <input type=\"text\" readonly=\"readonly\" id=\"n" + i + "\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strMateralName + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                strHtml.Append("申请数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width: 10%\">");
                strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                //strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                //strHtml.Append("库存数量：");
                //strHtml.Append("</td>");
                //strHtml.Append("<td align=\"left\">");
                //strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" readonly=\"readonly\" id=\"c" + i + "\" value=\"" + strNum + "\"/>");
                //strHtml.Append(" </td>");
                strHtml.Append("<td align=\"right\" style=\"width: 10%\" >");
                strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                strHtml.Append(" <input type=\"text\" id=\"p" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append("</td>");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            hidMaterialIDList.Value = "," + txtUserIdHid.Value + ",";
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();
        }
        #endregion

        #region  绑定数据
        private void BindDate(string id)
        {
            Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            purchase = bll.GetModel(id);
            endtime.Value = Convert.ToDateTime(purchase.END_TIME).ToShortDateString().Replace('/', '-');
            applytitel.Value = purchase.TITLE;
            Remark.Value = purchase.TITLE;
            if (Request["sign"] == "2")
            {
                Approver.Text = purchase.APPROVER_NAME;
                Notation.Text = purchase.NOTATION;
            }
            BindMaterial(id);
        }
        #endregion

        #region 按钮事件

        //保存
        protected void btnSend_Click(object sender, EventArgs e)
        {
            //if (txtSign.Text=="add")
            //{
            //    int userid = Convert.ToInt32(Session["USER_ID"].ToString().Trim());
            //    Model.Procurement.T_PUB_PURCHASE purchase = new Model.Procurement.T_PUB_PURCHASE();
            //    BLL.Procurement.T_PUB_PURCHASE bll = new BLL.Procurement.T_PUB_PURCHASE();
            //    Model.SysManage.T_PUB_USER user = new Model.SysManage.T_PUB_USER();
            //    BLL.SysManage.T_PUB_USER blluser = new BLL.SysManage.T_PUB_USER();
            //    user = blluser.GetModel(userid);
            //    purchase.BRANCH_ID = user.BRANCH_ID;
            //    //purchase.NUM = Convert.ToDecimal(txtnum.Text);
            //    //purchase.MATERIAL_ID = int.Parse(MaterialID.Value);
            //    purchase.APPLICANT = userid;
            //    purchase.END_TIME = Convert.ToDateTime(endtime.Value);
            //    purchase.REMARK = txtremark.Text;
            //    purchase.STATE = "1";
            //    DateTime date = DateTime.Now;
            //    purchase.APPLICANT_TIME = date;
            //    purchase.TITLE = txtpurchase.Text;
            //    bll.Add(purchase);
            //}
       }

        #endregion



    }
}